package com.laymi._admin._main;

import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import com.jfinal.core.Controller;
import com.jfinal.kit.JMap;
import com.jfinal.kit.Ret;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.laymi.common._util.GID;
import com.laymi.common._util.SqlKit;
import com.laymi.common._util.SqlUtil;
import com.laymi.common.model.Blog;
import com.laymi.common.model.GTable;
import com.laymi.common.model.GTableColumn;
import com.laymi.common.model.Note;

/**
 * 本 demo 仅表达最为粗浅的 jfinal 用法，更为有价值的实用的企业级用法
 * 详见 JFinal 俱乐部: http://jfinal.com/club
 * 
 * IndexController
 */
public class IndexController extends Controller {
	//首页
	public void index() {
		render("/WEB-INF/_admin/_main/index.html");
	}
	public void blogs() {
		renderJson(SqlUtil.searchPage(new Blog(), getParaMap(),"*", getParaToInt(0), 2));
	}
	public void delOne() {
		Db.deleteById("blog", getPara("id"));
		renderJson(Ret.ok("msg", "删除成功"));
	}
	public void delAll() {
		if (getPara("ids")!=null&&getPara("ids").split(",").length > 0) {  // idList 有可能为空，不加判断会出异常
			StringBuilder sql = new StringBuilder("delete from blog where id in");
			SqlKit.joinIds2(getPara("ids").split(","), sql);
			Db.update(sql.toString());
		}
		renderJson(Ret.ok("msg", "删除成功"));
	}
	public void activate() {
		if(getPara("status")!=null&&!"".equals(getPara("status"))){
			if("0".equals(getPara("status"))){
				Blog.dao.findById(getPara("id"))
				.setStatus("1").update();
				renderJson(Ret.ok("msg", "冻结成功"));
			}else{
				Blog.dao.findById(getPara("id"))
				.setStatus("0").update();
				renderJson(Ret.ok("msg", "激活成功"));
			}
		}
	}
	public void toadd() {
		render("/WEB-INF/_admin/_main/add.html");
	}
	public void doadd() {
		getModel(Blog.class,"")
		.setId(GID.getId())
		.save();
		renderJson(Ret.ok("msg", "添加成功"));
	}
	public void toedit() {
		setAttr("t", "edit");
		setAttr("e", Blog.dao.findById(getPara(0)));
		render("/WEB-INF/_admin/_main/edit.html");
	}
	public void tosee() {
		setAttr("t", "see");
		setAttr("e", Blog.dao.findById(getPara(0)));
		render("/WEB-INF/_admin/_main/edit.html");
	}
	public void doedit() {
		getModel(Blog.class,"")
		.update();
		renderJson(Ret.ok("msg", "修改成功"));
	}
}



